﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Anke.SHManage.MSSQLDAL;
using Anke.SHManage.Utility;
using Anke.SHManage.Model;

namespace Anke.SHManage.BLL.MobileBLL
{
    public class MobilePatientRecordBLL
    {
        private M_PatientRecordDAL m_PRDAL = new M_PatientRecordDAL();
        private MobileTaskDAL m_DAL = new MobileTaskDAL();

        //PAD上传病历信息
        public bool PADAddPatientRecord(string TaskCode, int PatientOrder, string EvenType, string TaskOrder
            , M_PatientRecord pr, M_PatientRecordAppend pra, M_PatientRecordCPR prc
            , List<M_PatientRecordDiag> prDiag, List<M_PatientRecordECGImpressions> prECG
            , List<M_PatientRecordRescue> prRescue, List<M_PatientRecordMeasure> prMeasure
            , List<M_PatientRecordDrug> prDrug, List<M_PatientRecordSanitation> prSanitation
            , List<M_PatientRecordLossDrug> prLossDrug, List<M_PatientRecordLossSanitation> prLossSanitation
            , string WorkCode, string PersonName, ref string errorMsg)
        {
            try
            {
                //2021-07-15 zch
                bool retBoole = false;

                if (pr == null)
                    pr = new M_PatientRecord();
                //if (pra == null)
                //    pra = new M_PatientRecordAppend();
                //if (prc == null)
                //    prc = new M_PatientRecordCPR();

                if (TaskCode == "")
                {
                    TaskCode = m_DAL.GetTaskCodeByTaskOrder(TaskOrder);
                }
                if (TaskCode == "")
                {
                    errorMsg = "任务流水号错误！未在任务中找到该流水号。";
                    return false;
                }
                pr.TaskCode = TaskCode;
                pr.PatientOrder = PatientOrder;
                pr.CallOrder = TaskOrder;//用户流水号
                pr.AgentWorkID = WorkCode;
                pr.AgentName = PersonName;
                pr.PatientVersion = EvenType;
                pr.IMEI = "PAD";
                pr.MedicalStateCode = 0;
                pr.FormCompleteLogo = false;//默认
                pr.SubmitLogo = false;
                pr.SubCenterIFSpotChecks = false;//默认分中心是否抽查
                pr.CenterIFSpotChecks = false;//默认中心是否抽查
                pr.LastUpdatePerson = PersonName;
                pr.LastUpdateTime = DateTime.Now;

                if (pra != null)
                {
                    pra.TaskCode = TaskCode;
                    pra.PatientOrder = PatientOrder;
                }
                object objpr = null;
                M_PatientRecordAppend mpra = null;
                M_PatientRecordCPR mprCPR = null;
                //M_PatientRecord mpr = null;
                if (prc != null)
                {
                    prc.TaskCode = TaskCode;
                    prc.PatientOrder = PatientOrder;
                }
                if (prRescue != null)
                {
                    foreach (M_PatientRecordRescue info in prRescue)
                    {
                        info.TaskCode = TaskCode;
                        info.PatientOrder = PatientOrder;
                    }
                }
                if (prMeasure != null)
                {
                    foreach (M_PatientRecordMeasure info in prMeasure)
                    {
                        info.TaskCode = TaskCode;
                        info.PatientOrder = PatientOrder;
                    }
                }
                if (prDrug != null)
                {
                    foreach (M_PatientRecordDrug info in prDrug)
                    {
                        info.TaskCode = TaskCode;
                        info.PatientOrder = PatientOrder;
                    }
                }
                if (prSanitation != null)
                {
                    foreach (M_PatientRecordSanitation info in prSanitation)
                    {
                        info.TaskCode = TaskCode;
                        info.PatientOrder = PatientOrder;
                    }
                }
                m_DAL.GetPADPatientInfo(TaskCode, PatientOrder, out objpr, out mpra, out mprCPR);
                if (objpr != null)
                {
                    //mpr = (M_PatientRecord)objpr;
                    //ReflectionUtility.CopyObjectProperty(mpr, pr);
                    //ReflectionUtility.CopyObjectProperty(mpra, pra);
                    //ReflectionUtility.CopyObjectProperty(mprCPR, prc);
                    retBoole = m_DAL.UpdatePAD(pr, pra, prc, prDiag, prECG, prRescue
                               , prMeasure, prDrug, prSanitation, prLossDrug, prLossSanitation);
                }
                else
                {
                    //20151211-注释，因为在传给PAD的列表里传过去了
                    //string HJTel = "";
                    //string Area = "";
                    //string EventType = "";
                    //new MobileTaskDAL().GetMobileTaskInfo(TaskCode, out HJTel, out Area, out EventType);
                    //pr.ForHelpTelephone = HJTel;
                    //pr.OriginalTaskType = EventType;
                    //pr.ForArea = Area;
                    pr.MedicalRecordGenerationTime = DateTime.Now;
                    if (prc != null)
                    {
                        prc.CenterIFAuditForXFFS = false;
                    }
                    retBoole = m_DAL.InsertPAD(pr, pra, prc, prDiag, prECG, prRescue, prMeasure, prDrug, prSanitation, prLossDrug, prLossSanitation);
                }
                return retBoole;
            }
            catch (Exception ex)
            {
                errorMsg = ex.Message;
                return false;
            }
        }

        #region 根据任务编码和序号获取病历全部信息
        public void GetPatientAndRescueInfo(string TaskCode, int PatientOrder, out M_PatientRecord info
            , out M_PatientRecordAppend pra, out M_PatientRecordCPR mprCPR, out List<M_PatientRecordRescue> prrs
           , out  List<M_PatientRecordMeasure> prms, out  List<M_PatientRecordDrug> prds, out  List<M_PatientRecordSanitation> prss)
        {
            info = null;
            object objpr = null;
            m_DAL.GetPADPatientInfo(TaskCode, PatientOrder, out objpr, out pra, out mprCPR);
            m_DAL.GetPRRescueMeasureInfos(TaskCode, PatientOrder, out prrs, out prms, out prds, out prss);//20151210增加取救治记录和措施等子表信息
            if (objpr != null)
            {
                info = (M_PatientRecord)objpr;
            }
        }
        #endregion

        #region 2016-12-13 zch 根据任务编码获取
        public int GetPatientOrderByTaskCode(string taskCode)
        {
            return m_DAL.GetPatientOrderByTaskCode(taskCode);
        }
        //2016-12-13 zch
        public int GetIFChargeByTaskCode(string taskCode, int PatientOrder)
        {
            return m_DAL.GetIFChargeByTaskCode(taskCode, PatientOrder);
        }

        #endregion
    }
}
